<?php

namespace core\exception;

use core\config\Config;


/**
 * 允许获取异常信息的公共异常基类
 * 注意抛出此类异常 即默认允许后端人员获取异常信息返回给前端
 * Class CommonException
 * @package infocenter\exception
 */
class CommonException extends \Exception
{

    private $data = [];

    public function __construct($message = '', $code = 0, $data = [])
    {
        $this->data = $data;
        parent::__construct($message, $code);
    }

    public function getData()
    {
        return $this->data;
    }


    public function handleException()
    {
        $out = ['code' => $this->getCode()];
        // 如果不是线上环境 直接将错误信息返回 方便调试
        if (!Config::isProduct()) {
            $out['msg'] = $this->__toString();
        } else {
            $out['msg'] = $this->getMessage();
        }
        if (!empty($this->data)) {
            $out['data'] = $this->getData();
        }
        return $out;
    }
}